<html>
<head><meta charset="utf-8"><title>Clobbering x87 registers · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html">Clobbering x87 registers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233068910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233068910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233068910">(Apr 04 2021 at 09:29)</a>:</h4>
<p>On x86_32 we currently have no way to indicate that x87 registers are clobbered, which effectively makes it unsound for <code>asm!</code> to call any other function. The x86 calling convention requires the x87 stack to be empty on function entry and exit, but currently LLVM may keep values in the x87 stack across <code>asm</code> blocks.</p>



<a name="233069111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233069111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233069111">(Apr 04 2021 at 09:33)</a>:</h4>
<p>There's 3 general approaches towards handling this:</p>
<ol>
<li>Expose x87 registers as <code>asm!</code> operands. I'd rather avoid doing this since these don't really act like normal registers (it's a stack of registers). There are complex <a href="https://github.com/llvm/llvm-project/blob/6e96de8abf272ee6c9b32b221149c2c04892b72a/llvm/lib/Target/X86/X86FloatingPoint.cpp#L1500">rules</a> for x87 constraints for LLVM's <code>inlineasm</code>.</li>
<li>Add an x86-specific <code>clobber_x87</code> option which acts as a blanket clobber for the entire x87 register stack.</li>
<li>Always mark x87 registers as clobbered.</li>
</ol>



<a name="233069182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233069182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233069182">(Apr 04 2021 at 09:34)</a>:</h4>
<p>I'm currently tending towards option 3 because honestly, nobody really cares about pre-SSE x86 any more.</p>



<a name="233069295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233069295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233069295">(Apr 04 2021 at 09:37)</a>:</h4>
<p>I agree that 1 is not a good option for the reason you mentioned. Option 2 makes it easy to forget the clobber. Even more so than regular register clobbers, as those are more visible and are more likely to cause a crash if you get them wrong. I too think option 3 is the best one.</p>



<a name="233070236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233070236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233070236">(Apr 04 2021 at 09:58)</a>:</h4>
<p>I'd like to argue against the nobody cares. While people won't wrote production software for these targets that often there is at least one situation where x86 or earlier comes up often - architecture/os university classes.</p>



<a name="233070259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233070259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233070259">(Apr 04 2021 at 09:59)</a>:</h4>
<p>Well sure, but at most the effect is slightly reduced performance since LLVM needs to spill FP registers to the stack.</p>



<a name="233078598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233078598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233078598">(Apr 04 2021 at 12:35)</a>:</h4>
<p>yeah, I didn't mean to suggest that the solution is not appropriate, its just that people still care about those targets at least a little bit ^^</p>



<a name="233084140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233084140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233084140">(Apr 04 2021 at 14:22)</a>:</h4>
<p>Seems like we could always do number 3 and then add a <code>noclobber_x87</code> option if you're willing to handle things more carefully.</p>



<a name="233098611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233098611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233098611">(Apr 04 2021 at 17:09)</a>:</h4>
<p>This isn't a long-term strategy though: consider the recently added <code>tmm[0-7]</code> registers on x86 with the AMX extension. There's currently no way to mark those as clobbered.</p>



<a name="233098629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233098629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233098629">(Apr 04 2021 at 17:09)</a>:</h4>
<p>I'm now considering going for option 4 which is to add support for wildcard clobbers (clobber all, clobber for a call to "C" abi function, etc).</p>



<a name="233100532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Clobbering%20x87%20registers/near/233100532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Clobbering.20x87.20registers.html#233100532">(Apr 04 2021 at 17:44)</a>:</h4>
<p>Hugely in favor of that solution.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>